package com.kingsoft.demo.jdbc;

import java.sql.*;

/**
 * 获取表的元数据信息
 * 元数据：表示数据的数据
 */
public class TestB {

	public void test(){
		
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			Connection con=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORCL","scott","tiger");
			Statement st=con.createStatement();
			ResultSet rs=st.executeQuery("select * from emp");
			
			//从ResultSet 结果集中提取元数据信息--ResultSetMetaData
			ResultSetMetaData rsmd =rs.getMetaData();
			int count =rsmd.getColumnCount();
			System.out.println("count:"+count);
			
			for(int i=1;i<=count;i++){
				
				System.out.println(rsmd.getColumnName(i)+":"+rsmd.getColumnTypeName(i)+"("+
						rsmd.getPrecision(i)+")");
				System.out.println(rsmd.getColumnType(i));
				
				
			}
			
			
			
			
			rs.close();
			st.close();
			con.close();						
			
		} catch (ClassNotFoundException e) {			
			e.printStackTrace();
		} catch (SQLException e) {			
			e.printStackTrace();
		}
		
	}
	
	public void test2(){
		
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","admin");
			Statement st=con.createStatement();
			ResultSet rs=st.executeQuery("select * from emp");
			
			//从ResultSet 结果集中提取元数据信息--ResultSetMetaData
			ResultSetMetaData rsmd =rs.getMetaData();
			int count =rsmd.getColumnCount();
			System.out.println("count:"+count);
			
			for(int i=1;i<=count;i++){
				
				System.out.println(rsmd.getColumnName(i)+":"+rsmd.getColumnTypeName(i)+"("+
						rsmd.getPrecision(i)+")");
				System.out.println(rsmd.getColumnType(i));
				
				
			}
			
			
			
			
			rs.close();
			st.close();
			con.close();						
			
		} catch (ClassNotFoundException e) {			
			e.printStackTrace();
		} catch (SQLException e) {			
			e.printStackTrace();
		}
		
	}
	public static void main(String[] args) {
		new TestB().test2();
	}
}
